[HybridWebView] Fix some issues with the interception, typescript and other features#29829
[HybridWebView] Fix some issues with the interception, typescript and other features#29829
Conversation
Android: Observed lower case on some OS versions (ie 28) Windows: Header lookup is case insensitive macOS/iOS: CI will inform
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a new test case to verify that the HybridWebView can intercept requests and read headers in a case-insensitive manner, addressing issues observed on various OS versions.
- Added a new test method RequestsCanBeInterceptedAndCaseInsensitiveHeadersRead.
- Incorporated conditional test data for different platforms based on header handling capabilities.
Comments suppressed due to low confidence (1)
src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests.cs:771
- [nitpick] Consider refactoring the repetitive try-catch blocks used for header value retrieval into a helper function or loop to reduce duplication and improve maintainability.
try { headerValues["X-Echo-Name"] = e.Headers["X-Echo-Name"]; }
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
| internal IReadOnlyDictionary<string, string>? GetRequestHeaders() => new WrappedHeadersDictionary(Request.Headers); | ||
|
|
||
| class WrappedHeadersDictionary : IReadOnlyDictionary<string, string> |
There was a problem hiding this comment.
The previous way to use wrappers was me being "so clever" and "avoiding allocations" and "reducing copies"... But yeah, that was not sensible since the amount of things that would happen was so little. In order to hit this path, the user has to actually request the headers.
For all other cases, this doesn't do anything.
| internal IReadOnlyDictionary<string, string> GetRequestHeaders() => | ||
| _headers ??= Request.RequestHeaders is { } rh | ||
| ? new Dictionary<string, string>(rh, StringComparer.OrdinalIgnoreCase) | ||
| : new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); |
There was a problem hiding this comment.
Unfortunately, depending on the server and how the headers are populated, Android sometimes converts the headers to lower case. So we always will ned a case-insensitive.
Windows was already case insensitive, so this just makes it all consistent.
| } | ||
| // Determine the mechanism to receive messages from the host application. | ||
| if (window.chrome?.webview?.addEventListener) { | ||
| if (window.chrome && window.chrome.webview && window.chrome.webview.addEventListener) { |
There was a problem hiding this comment.
The Android Chromeis weird. It suports async functions ahead of its time, but then also does not support ?.. I am at a loss.
|
/backport to release/10.0.1xx-preview5 |
|
Started backporting to release/10.0.1xx-preview5: https://github.com/dotnet/maui/actions/runs/15473805094 |
rmarinho
left a comment
There was a problem hiding this comment.
Ok seems we are green now! Thanks @mattleibow
|
I backported but I also have this branch to just bring latest changes to the release branch .. go wild or go home |
|
/backport to main |
|
Started backporting to main: https://github.com/dotnet/maui/actions/runs/15500641832 |
|
@mattleibow backporting to "main" failed, the patch most likely resulted in conflicts: $ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch
Applying: Sometimes the OS changes the header casing
Using index info to reconstruct a base tree...
M src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests.cs
CONFLICT (content): Merge conflict in src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Sometimes the OS changes the header casing
Error: The process '/usr/bin/git' failed with exit code 128Please backport manually! |

This pull request introduces several improvements and fixes to the
HybridWebViewcomponent, focusing on case-insensitive header handling, improving compatibility across platforms, refactoring JavaScript code, and enhancing testing. The most significant changes include updates to header dictionaries to support case-insensitivity, refactoring the JavaScript code for better readability and maintainability, and adding platform-specific compatibility checks in tests.Case-insensitive header handling:
src/Controls/src/Core/HybridWebView/HybridWebViewWebResourceRequestedEventArgs.cs: UpdatedHeadersandQueryParametersdictionaries to useStringComparer.OrdinalIgnoreCaseandStringComparer.Ordinal, respectively, ensuring case-insensitive handling of headers and query parameters.src/Controls/src/Core/HybridWebView/PlatformHybridWebViewWebResourceRequestedEventArgs.cs: RefactoredGetRequestHeadersimplementations across platforms to useStringComparer.OrdinalIgnoreCasefor consistent case-insensitive header handling. [1] [2] [3]JavaScript code refactoring:
src/Core/src/Handlers/HybridWebView/HybridWebView.js: Refactored JavaScript functions such asSendRawMessage,InvokeDotNet, and__InvokeJavaScriptinto standalone functions, and restructured theHybridWebViewobject for better readability and maintainability. Removed optional chaining for compatibility. [1] [2] [3] [4]Platform-specific compatibility in tests:
src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests.cs: Added checks to skip tests on older Android versions and certain platforms where features like custom schemes or HTTPS interception are not supported. Improved test logic for validating case-insensitive headers. [1] [2] [3]TypeScript updates:
src/Core/src/Handlers/HybridWebView/HybridWebView.ts: Updated TypeScript code to remove optional chaining and ensure compatibility with platforms that do not support it.These changes collectively enhance the
HybridWebViewcomponent's functionality, improve cross-platform compatibility, and ensure robust testing practices.